package com.test.group6.util;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.test.group6.mapper.AddEnterpriseMapper;
import com.test.group6.mapper.AddWorkesMapper;
@Component
public class CreateId {
	@Autowired
	private AddEnterpriseMapper addEnterpriseMapper;
	@Autowired
	private AddWorkesMapper addWorkesMapper;
	
	public static CreateId createId;
	@PostConstruct
	public void init() {
		createId=this;
	}
	
    public String createId(String idType) {
    	String id="";
    	String headPart="";
    	String idPart="";
    	if(idType.equals("Enterprise")) {
    		String enterpriseId=createId.addEnterpriseMapper.getMaxIdInEnterprise();
    		if(enterpriseId!=null&&!enterpriseId.equals("")) {
    			idPart=CreateId.idAdd(enterpriseId.substring(1));
    		}else {
    			idPart="0000000001";
    		}
    		headPart="C";
    	}else if(idType.equals("Workes")) {
    		String workesId=createId.addWorkesMapper.getMaxIdInWorkes();
    		if(workesId!=null&&!workesId.equals("")) {
    			idPart=CreateId.idAdd(workesId.substring(1));
    		}else {
    			idPart="0000000001";
    		}
    		headPart="M";
    	}
		id=headPart+idPart;
    	return id;
    }
    
    private static String idAdd(String id) {
    	int idLenghth=id.length();
    	int intId=Integer.parseInt(id);
    	intId++;
    	String idPart="";
    	if(idLenghth==10) {
    		idPart=""+intId;
    		idLenghth=idPart.length();
    		while(idLenghth<10) {
    			idPart="0"+idPart;
    			idLenghth=idPart.length();
    		}
    	}else {
    		idPart="id达到上限";
    	}
    	
    	return idPart;
    }
}
